package com.vk.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vk.project.annotation.AuthCheck;
import com.vk.project.common.BaseResponse;
import com.vk.project.common.ErrorCode;
import com.vk.project.common.ResultUtils;
import com.vk.project.exception.BusinessException;
import com.vk.project.mapper.UserInterfaceInfoMapper;
import com.vk.project.model.vo.InterfaceInfoVO;
import com.vk.project.service.InterfaceInfoService;
import com.vk.vkapicommon.model.entity.InterfaceInfo;
import com.vk.vkapicommon.model.entity.UserInterfaceInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName : AnalysisController
 * @Description : 分析控制器
 * @Author : LYQ
 * @Date: 2023-12-08 15:23
 */
@RestController
@RequestMapping("/analysis")
public class AnalysisController {

    @Autowired
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Autowired
    private InterfaceInfoService interfaceInfoService;

    /**
     * 查询接口调用次数最多的接口信息
     *
     * @return
     */
    @GetMapping("/top/interface/invoke")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<InterfaceInfoVO>> listTopInvokeInterfaceInfo() {
        // 查询调用次数最多的3个接口信息
        List<UserInterfaceInfo> userInterfaceInfoList = userInterfaceInfoMapper.listTopInvokeInterfaceInfo(3);
        // 将查询到的接口信息按照接口信息ID进行分组
        Map<Long, List<UserInterfaceInfo>> interfaceInfoIdObjMap = userInterfaceInfoList.stream()
                .collect(Collectors.groupingBy(UserInterfaceInfo::getInterfaceInfoId));
        // 创建QueryWrapper对象，用于查询接口信息
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        // 将查询到的接口信息ID添加到QueryWrapper中
        queryWrapper.in("id", interfaceInfoIdObjMap.keySet());
        // 查询接口信息
        List<InterfaceInfo> list = interfaceInfoService.list(queryWrapper);
        // 如果查询到的接口信息为空，则抛出异常
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        // 创建InterfaceInfoVO对象列表，用于返回给前端
        List<InterfaceInfoVO> interfaceInfoVOList = list.stream().map(interfaceInfo -> {
            // 创建InterfaceInfoVO对象
            InterfaceInfoVO interfaceInfoVO = new InterfaceInfoVO();
            // 将接口信息的数据复制到InterfaceInfoVO对象
            BeanUtils.copyProperties(interfaceInfo, interfaceInfoVO);
            // 获取接口信息的调用次数
            int totalNum = interfaceInfoIdObjMap.get(interfaceInfo.getId()).get(0).getTotalNum();
            // 将调用次数设置到InterfaceInfoVO对象中
            interfaceInfoVO.setTotalNum(totalNum);
            return interfaceInfoVO;
        }).collect(Collectors.toList());
        // 返回InterfaceInfoVO对象列表
        return ResultUtils.success(interfaceInfoVOList);
    }
}
